package com.yy.controller.admin;

import com.yy.dto.DishDTO;
import com.yy.dto.DishPageQueryDTO;
import com.yy.entity.Dish;
import com.yy.result.PageResult;
import com.yy.result.Result;
import com.yy.service.DishService;
import com.yy.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public Result addDish(@RequestBody DishDTO dto) {
        dishService.addDish(dto);
        redisTemplate.delete("dish_" + dto.getCategoryId());
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(DishPageQueryDTO dto) {
        PageResult pageResult = dishService.page(dto);
        return Result.success(pageResult);
    }

    @PutMapping
    public Result updateDish(@RequestBody DishDTO dto) {
        dishService.update(dto);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        dishService.delete(ids);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id) {
        DishVO dishVO = dishService.selectById(id);
        return Result.success(dishVO);
    }

    @GetMapping("/list")
    public Result selectByCategoryId(Long categoryId) {
        List<Dish> dishList = dishService.selectByCategoryId(categoryId);
        return Result.success(dishList);
    }

    @PostMapping("/status/{status}")
    public Result enableOrDisable(@PathVariable Integer status, Long id) {
        dishService.enableOrDisable(status, id);
        return Result.success();
    }
}
